<?php
/**
 * CK框架 smtp邮件类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class smtp
{
	/**
	 * smtp资源
	 * @var rescorue
	 */
	private $_smtpFp;
	/**
	 * 报头结束后缀
	 * @var unknown_type
	 */
	private $_headerEnd = "\r\n";
	private $_fromEmail;
	/**
	 * smtp进行登录
	 * @param string $smtpHost
	 * @param string $smtpPort
	 * @param string $smtpEmail
	 * @param string $userName
	 * @param string $passWord
	 */
	public function login($smtpHost,$smtpPort,$smtpEmail,$userName,$passWord)
	{
		$mailHeader = array
		(
				array('EHLO '.$smtpHost,'220,250','HELO error:'),
				array('AUTH LOGIN','334','AUTH ERROR:'),
				array(base64_encode($userName),'334','AUTHENTIFICATION error:'),
				array(base64_encode($passWord),'235','AUTHENTIFICATION error:'),
				array('MAIL FROM: <'.$smtpEmail.'>','250','MAIL FROM error:'),
		);
		//进行socket登录测试跑出异常
		$this->_smtpFp = fsockopen($smtpHost,$smtpPort,$errorNo,$errorString);
		if(!$this->_smtpFp)
		{
			throw new smtpNotContentException($smtpHost,$errorNo,$errorString);	
		}
		while($result = fgets($this->_smtpFp, 1024))
		{
			if(substr($result,3,1) == ' ')
			break;
		}
		$this->_fromEmail = $smtpEmail;
		$this->sendHeader($mailHeader);
	}
	/**
	 * 发送邮件
	 * @param string $emali 邮件地址
	 * @param string $content 邮件内容
	 */
	public function send($emali,$title,$content)
	{
		$smtp_pwd[] = array('RCPT TO:<'.$emali.'>','250','RCPT TO error:');
		$smtp_pwd[] = array('DATA','354','DATA error:');
		$smtp_pwd[] = array('From: '.$this->_fromEmail,'','');
		$smtp_pwd[] = array('To: <'.$emali.'>','','');
		$smtp_pwd[] = array('Subject: '.$title,'','');
		//其他HEARDER报头信息
		$smtp_pwd[] = array('Content-Type: text/html; charset="utf-8"','','');
		$smtp_pwd[] = array('Content-Transfer-Encoding: base64','','');
		$smtp_pwd[] = array('','','');
		$smtp_pwd[] = array(base64_encode($content.$this->_headerEnd),'','');
		$smtp_pwd[] = array('.','250','DATA(end)error: ');
		$this->sendHeader($smtp_pwd);
	}
	public function close()
	{
		$header = array(
				array('QUIT','221','QUIT Error:'),
				);
		$this->sendHeader($header);
		fclose($this->_smtpFp);
	}
	/**
	 * 发送报头信息
	 * @param array $header
	 */
	private function sendHeader($header)
	{
		if($header) foreach ($header as $headerValue)
		{
			//发送报头信息
			fputs($this->_smtpFp, $headerValue[0].$this->_headerEnd);
			if($headerValue[1])
			{
				while ($result = fgets($this->_smtpFp,1024))
				{
					if(substr($result, 3,1) == ' ')
					{
						break;
					}
					//判断是否失败
					if(!strstr($headerValue[1], substr($result, 0,3)))
					{
						throw new sendHeaderException($headerValue[0].'报头错误:'.$headerValue[2].$result);
					}
				}	
			}
		}
	}
}


/**
 * CK框架 不能连接SMTP服务器异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class smtpNotContentException extends Exception
{
	private $_smtpHost;
	private $_errorNo;
	private $_errorStr;
	public function __construct($smtpHost,$errorNo,$errorStr)
	{
		$this->_smtpHost = $smtpHost;
		$this->_errorNo = $errorNo;
		$this->_errorStr = $errorStr;	
	}
	public function __toString()
	{
		return '连接'.$this->_smtpHost.'失败。原因：'.$this->_errorStr.'.ErrorNum:'.$this->_errorStr;
	}
}


/**
 * CK框架 发送报头被服务器拒绝的异常信息
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class sendHeaderException extends Exception
{
	
}